// pages/user/mobile/change.js
import {
  getAuthCode,
  authMobile,
  changeMobile
} from '../../../api/user.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nextStep: false,
    authBtnText: '发送验证码',
    authStatus: 'theme',
    timeCount: 60,
    timer: '', // 定时器
    oldPhone: '',
    newPhone: '',
    authCode: '', // 输入框验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      oldPhone: getApp().globalData.userInfo.mobile,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
  },

  /**
   * 输入验证码
   */
  onInputAc(e) {
    this.setData({
      authCode: e.detail.value.trim()
    })
  },

  onInputNewPhone(e) {
    this.setData({
      newPhone: e.detail.value.trim()
    })
  },

  /**
   * 发送验证码
   */
  onSendAuthBtn() {
    let phone = this.data.nextStep ? this.data.newPhone : this.data.oldPhone;
    // 绑定新手机号
    if (this.data.nextStep) {
      if (!phone) {
        this.showToast('请输入手机号');
        return;
      } else if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(phone)) {
        this.showToast('手机号格式不正确');
        return;
      }
    }

    //  获取验证码
    getAuthCode(phone);

    let _count = this.data.timeCount;
    if (this.data.timer === '') {
      this.data.timer = setInterval(() => {
        if (_count == 0) {
          // 重置定时器
          clearInterval(this.data.timer);
          this.setData({
            authBtnText: '发送验证码',
            authStatus: 'theme',
            timer: ''
          })
          return;
        }
        _count -= 1;
        this.setData({
          authBtnText: '重新发送(' + _count + 's)',
          authStatus: 'disable',
        });
      }, 1000);
    }
  },

  /**
   * 验证/完成
   */
  onSureBtn() {
    if (!this.data.nextStep) {
      if (!this.data.authCode) {
        this.showToast('请输入验证码');
        return;
      }
      // 验证手机号
      authMobile(this.data.authCode, this.data.oldPhone).then(res => {
        if (res.isRight === 1) {
          this.onNext();
        } else {
          wx.showToast({
            title: '验证码输入错误',
            icon: 'none',
          })
        }
      })
    } else {
      let phone = this.data.newPhone;
      if (!phone) {
        this.showToast('请输入手机号');
        return;
      } else if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(phone)) {
        this.showToast('手机号格式不正确');
        return;
      }
      if (!this.data.authCode) {
        this.showToast('请输入验证码');
        return;
      }

      // 绑定新手机
      changeMobile(this.data.authCode, this.data.newPhone).then(() => {
        this.showToast('绑定成功')
        getApp().globalData.userInfo.mobile = this.data.newPhone;
        let timer = setTimeout(() => {
          clearTimeout(timer);
          wx.navigateBack({})
        }, 2000);
      })
    }
  },

  onNext() {
    // 重置定时器
    clearInterval(this.data.timer);
    this.data.timer = '';
    this.setData({
      authBtnText: '发送验证码',
      authCode: '',
      authStatus: 'theme',
      nextStep: true
    });
  },

  /**
   * 吐司
   * @param {string} msg 
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

})